from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Asset(models.Model):
    """    所有资产的共有数据表    """
    asset_type_choice = (
        ('server', '服务器'),
        ('networkdevice', '网络设备'),
    )

    asset_status = (
        (0, '运营'),
        (1, '闲置'),
        (2, '待启用'),
        (3, '故障'),
        (4, '备用'),
        )

    asset_business_unit = (
        (0, '领骏'),
        (1, '驰联'),
        )

    asset_type = models.CharField(choices=asset_type_choice, max_length=64, default='server', verbose_name=u"资产类型")
    name = models.CharField(max_length=64, unique=True, verbose_name=u"资产编号")     # 不可重复
    sn = models.CharField(max_length=128, unique=True, verbose_name=u"资产序列号")  # 不可重复
    hostname = models.CharField(verbose_name=u'设备命名', max_length=128, null=True,blank=True)
    business_unit = models.SmallIntegerField(choices=asset_business_unit, default=0,null=True, blank=True, verbose_name=u'所属业务')
    status = models.SmallIntegerField(choices=asset_status, default=0, verbose_name=u'设备状态')
    manufacturer = models.ForeignKey('Manufacturer',on_delete=models.CASCADE, null=True, blank=True, verbose_name=u'厂商')
    modelnumber = models.ForeignKey('Modelnumber',on_delete=models.CASCADE, null=True,blank=True)
    manage_ip = models.GenericIPAddressField(null=True,blank=True, verbose_name=u'管理IP')
    operation = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name=u'维护人', null=True, blank=True)
    idc = models.ForeignKey('DataCenter',on_delete=models.CASCADE, null=True, blank=True, verbose_name=u'所在机房')
    machineRoom = models.ForeignKey('MachineRoom', on_delete=models.CASCADE, null=True, blank=True, verbose_name=u'所在机房网络模块')
    cabinet = models.ForeignKey('Cabinet',on_delete=models.CASCADE,null=True,blank=True,verbose_name=u'机柜')
    high = models.IntegerField(verbose_name=u"高度(U)",blank=True)
    application = models.CharField(verbose_name=u'设备用途',default='', max_length=128,blank=True)
    purchase_day = models.DateField(null=True, blank=True, verbose_name=u"购买日期")
    expire_day = models.DateField(null=True, blank=True, verbose_name=u"过保日期")
    memo = models.TextField(blank=True,default='', verbose_name=u'备注')
    create_date = models.DateTimeField(blank=True, auto_now_add=True,verbose_name=u'创建日期')
    m_time = models.DateTimeField(auto_now=True, verbose_name=u'更新日期')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'资产总表'
        verbose_name_plural = u"资产总表"
        ordering = ['-create_date']

class Server(models.Model):
    """服务器设备表"""
    sub_asset_type_choice = (
        (0, u'PC机'),
        (1, u'服务器'),
    )

    created_by_choice = (
        ('auto', u'自动添加'),
        ('manual', u'手工录入'),
    )

    asset = models.OneToOneField('Asset',on_delete=models.CASCADE)  # 非常关键的一对一关联！
    server_type = models.SmallIntegerField(choices=sub_asset_type_choice, default=0, verbose_name=u"服务器类型")
    intranet_ip = models.GenericIPAddressField(verbose_name=u'内网IP', blank=True, null=True)
    internet_ip = models.GenericIPAddressField(verbose_name=u'外网IP', blank=True, null=True)
    created_by = models.CharField(choices=created_by_choice, max_length=32, default='manual', verbose_name=u"添加方式")
    hosted_on = models.ForeignKey('self',on_delete=models.CASCADE, related_name='hosted_on_server',blank=True, null=True, verbose_name=u"宿主机")  # 虚拟机专用字段
    model = models.CharField(max_length=128, null=True, blank=True, verbose_name=u'服务器型号')
    raid_type = models.CharField(max_length=512, blank=True, null=True, verbose_name=u'Raid类型')
    os_type = models.CharField(verbose_name=u'操作系统类型', max_length=64, blank=True, null=True)
    os_distribution = models.CharField(verbose_name=u'发行版本', max_length=64, blank=True, null=True)
    os_release = models.CharField(verbose_name=u'操作系统版本', max_length=64, blank=True, null=True)

    def __str__(self):
        return self.asset.name

    class Meta:
        verbose_name = u'服务器'
        verbose_name_plural = u"服务器"

class NetworkDevice(models.Model):
    """网络设备表"""
    sub_asset_type_choice = (
        (0, '路由器'),
        (1, '交换机'),
        (2, '防火墙'),
        (3, 'DWDM'),
    )

    asset = models.OneToOneField('Asset',on_delete=models.CASCADE)
    networkdevice_type = models.SmallIntegerField(choices=sub_asset_type_choice, default=0, verbose_name=u"网络设备类型")
    port_num = models.SmallIntegerField(null=True, blank=True, verbose_name=u"端口个数")
    os = models.CharField(verbose_name=u'OS', max_length=64, null=True,blank=True)

    def __str__(self):
        return self.asset.name

    class Meta:
        verbose_name = u'网络设备'
        verbose_name_plural = u"网络设备"

class Ports(models.Model):
    """设备端口表"""
    port_type_choice = (
        (0, u'网络设备互联'),
        (1, u'服务器互联'),
        (2, u'客户互联'),
        (3, u'待定'),
    )

    port_status_choice = (
        (0, '运营'),
        (1, '闲置'),
        (2, '故障'),
    )    

    name = models.CharField(verbose_name=u'端口命名', max_length=64)
    port_type = models.SmallIntegerField(choices=port_type_choice,verbose_name=u"端口类型")
    port_status = models.SmallIntegerField(choices=port_status_choice,default=0,verbose_name=u"端口状态")
    networkdevice = models.ForeignKey('Asset',on_delete=models.CASCADE, null=False, blank=False, verbose_name=u'所属设备')
    connect_server = models.ForeignKey('Server',on_delete=models.CASCADE, null=True, blank=True,verbose_name=u'互联服务器')
    connect_port = models.ForeignKey('self',on_delete=models.CASCADE,blank=True,null=True,verbose_name=u'对端设备端口')
    connect_user = models.CharField(verbose_name=u'对接客户名称', max_length=128)
    memo = models.CharField(verbose_name=u'备注', max_length=128, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '端口'
        verbose_name_plural = "端口"

class IpNet(models.Model):
    """网段表"""
    ipnet_status_choice = (
        (0, '运营'),
        (1, '待分配'),
        (2, '已分配'),
        (3, '保留'),
    )    
    ipnet_type_choice =(
        (0, '业务网段'),
        (1, '互联网段'),
    )
    split_status_choice =(
        (0, '是'),
        (1, '否'),
    )
    ipnet_business_type =(
        (0, 'IDC'),
        (1, 'IXP'),
        (2, '管理'),
    )

    name = models.CharField(verbose_name=u'网段名称', unique=True,max_length=64)
    netmask = models.GenericIPAddressField(verbose_name=u'掩码')
    gw_ip = models.GenericIPAddressField(null=True,blank=True, verbose_name=u'网关IP')
    isp = models.CharField(max_length=128,null=True,blank=True,verbose_name=u"网段运营商")
    ipnet_status = models.SmallIntegerField(choices=ipnet_status_choice,verbose_name=u"网段状态")    
    ipnet_type = models.SmallIntegerField(choices=ipnet_type_choice,verbose_name=u"网段类型")
    business_type = models.SmallIntegerField(choices=ipnet_business_type,verbose_name=u"业务类型")
    split_status = models.SmallIntegerField(choices=split_status_choice,verbose_name=u'是否完成裂解')
    vlan = models.SmallIntegerField(verbose_name=u"VLAN",null=True,blank=True,)
    ipnet_customer = models.CharField(verbose_name=u'对接客户名称', max_length=128)
    memo = models.CharField(verbose_name=u'备注', max_length=128, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '网段'
        verbose_name_plural = "网段"

class IpAddress(models.Model):
    """网段表"""
    ipaddress_status_choice = (
        (0, '运营'),
        (1, '待分配'),
        (2, '已分配'),
        (3, '保留'),
    )    

    name = models.GenericIPAddressField(verbose_name=u'IP名称',unique=True,max_length=64)
    netmask = models.GenericIPAddressField(verbose_name=u'掩码')
    ipnet = models.ForeignKey('IpNet',on_delete=models.CASCADE,verbose_name=u"所属网段")
    isp = models.CharField(max_length=128,null=True,blank=True,verbose_name=u"IP运营商")
    ipaddress_status = models.SmallIntegerField(choices=ipaddress_status_choice,verbose_name=u"ip地址状态")    
    vlan = models.SmallIntegerField(verbose_name=u"VLAN",null=True,blank=True)
    asset = models.ForeignKey('Asset',on_delete=models.CASCADE,verbose_name=u"分配设备",null=True,blank=True)
    ipaddress_customer = models.CharField(verbose_name=u'对接客户名称', max_length=128)
    memo = models.CharField(verbose_name=u'备注', max_length=128, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = 'IP地址'
        verbose_name_plural = "IP地址"

class Manufacturer(models.Model):
    """厂商表"""

    name = models.CharField(verbose_name=u'厂商名称', max_length=64, unique=True)
    telephone = models.CharField(verbose_name=u'支持电话', max_length=30, blank=True, null=True)
    memo = models.CharField(verbose_name=u'备注', max_length=128, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '厂商'
        verbose_name_plural = "厂商"

class Modelnumber(models.Model):
    """型号表"""
    model_type_choice = (
        ('server', '服务器'),
        ('networkdevice', '网络设备'),
        ('dwdmdevice', '波分设备'),
    )
    name = models.CharField(verbose_name=u'型号名称', max_length=64, unique=True)
    model_type = models.CharField(choices=model_type_choice, max_length=64, default='server', verbose_name="类型")
    memo = models.CharField(verbose_name=u'备注', max_length=128, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '型号'
        verbose_name_plural = "型号"


class Cabinet(models.Model):
    """
    机柜表
    """
    power_status_choice = (
        (0, '是'),
        (1, '否'),
    )
    user_self_choice = (
        (0, '是'),
        (1, '否'),
    )

    datacenter = models.ForeignKey('DataCenter',on_delete=models.CASCADE, null=True,blank=True)
    machineroom = models.ForeignKey('MachineRoom', on_delete=models.SET_NULL, null=True, blank=True, verbose_name=u"所属机房")
    name = models.CharField(max_length=128, verbose_name=u"机柜编号")
    slotcount = models.IntegerField(null=True, blank=True, verbose_name=u"槽位数(U)", default=0)
    status = models.SmallIntegerField(choices=power_status_choice, verbose_name=u'是否开电', default=0)
    user = models.SmallIntegerField(choices=user_self_choice,verbose_name=u'是否自用',default=0)
    power = models.IntegerField(null=True, blank=True, verbose_name=u"电力(A)", default=0)
    memo = models.CharField(verbose_name=u'备注', max_length=128, blank=True, null=True)

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name

    class Meta:
        verbose_name = u'机柜'
        verbose_name_plural = u'机柜'
        unique_together = ('machineroom','name')

class MachineRoom(models.Model):
    """
    机房管理单元
    """
    datacenter = models.ForeignKey('DataCenter', related_name="related_rooms",on_delete=models.SET_NULL, null=True, blank=True, verbose_name=u"所属中心")
    name = models.CharField(max_length=100, verbose_name=u"机房管理单元名称",unique=True)
    address = models.CharField(max_length=200, null=True, blank=True, verbose_name=u"机房地址")
    admin = models.ForeignKey(User, verbose_name=u'网络模块负责人', null=True, blank=True, on_delete=models.SET_NULL)
    contact = models.CharField(verbose_name=u'联系电话', max_length=30, blank=True, null=True)
    memo = models.CharField(verbose_name=u'备注', max_length=128, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name_plural = u"机房管理单元"
        verbose_name = u"机房管理单元"


class DataCenter(models.Model):
    """
    数据中心
    """
    name = models.CharField(max_length=100, verbose_name=u"数据中心",unique=True)
    shortname = models.CharField(max_length=32,verbose_name=u"机房简称",unique=True)
    isp = models.CharField(max_length=128,verbose_name=u"机房运营商")
    admin = models.ForeignKey(User,verbose_name=u'负责人', null=True, blank=True, on_delete=models.SET_NULL)
    contactnumber = models.CharField(verbose_name=u'联系电话', max_length=30, null=True, blank=True)
    address = models.CharField(max_length=200, null=True, blank=True, verbose_name=u"机房地址")
    memo = models.CharField(verbose_name=u'备注', max_length=128, blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '机房'
        verbose_name_plural = "机房"

class CPU(models.Model):
    """CPU组件"""

    asset = models.OneToOneField('Asset',on_delete=models.CASCADE)  # 设备上的cpu肯定都是一样的，所以不需要建立多个cpu数据，一条就可以，因此使用一对一。
    cpu_model = models.CharField(verbose_name=u'CPU型号', max_length=128, blank=True, null=True)
    cpu_count = models.PositiveSmallIntegerField(verbose_name=u'物理CPU个数', default=1)
    cpu_core_count = models.PositiveSmallIntegerField(verbose_name=u'CPU核数', default=1)

    def __str__(self):
        return self.asset.name + ":   " + self.cpu_model

    class Meta:
        verbose_name = 'CPU'
        verbose_name_plural = "CPU"

class RAM(models.Model):
    """内存组件"""

    asset = models.ForeignKey('Asset',on_delete=models.CASCADE)  # 只能通过外键关联Asset。否则不能同时关联服务器、网络设备等等。
    sn = models.CharField(verbose_name=u'SN号', max_length=128, blank=True, null=True)
    model = models.CharField(verbose_name=u'内存型号', max_length=128, blank=True, null=True)
    manufacturer = models.CharField(verbose_name=u'内存制造商', max_length=128, blank=True, null=True)
    slot = models.CharField(verbose_name=u'插槽', max_length=64)
    capacity = models.IntegerField(verbose_name=u'内存大小(GB)', blank=True, null=True)

    def __str__(self):
        return '%s: %s: %s: %s' % (self.asset.name, self.model, self.slot, self.capacity)

    class Meta:
        verbose_name = u'内存'
        verbose_name_plural = u"内存"
        unique_together = ('asset', 'slot')  # 同一资产下的内存，根据插槽的不同，必须唯一

class Disk(models.Model):
    """存储设备"""

    disk_interface_type_choice = (
        ('SATA', 'SATA'),
        ('SAS', 'SAS'),
        ('SCSI', 'SCSI'),
        ('SSD', 'SSD'),
        ('unknown', 'unknown'),
    )

    asset = models.ForeignKey('Asset',on_delete=models.CASCADE)
    sn = models.CharField(verbose_name=u'硬盘SN号', max_length=128)
    slot = models.CharField(verbose_name=u'所在插槽位', max_length=64, blank=True, null=True)
    model = models.CharField(verbose_name=u'磁盘型号', max_length=128, blank=True, null=True)
    manufacturer = models.CharField(verbose_name=u'磁盘制造商', max_length=128, blank=True, null=True)
    capacity = models.FloatField(verbose_name=u'磁盘容量(GB)', blank=True, null=True)
    interface_type = models.CharField(verbose_name=u'接口类型', max_length=16, choices=disk_interface_type_choice, default='unknown')

    def __str__(self):
        return '%s:  %s:  %s:  %sGB' % (self.asset.name, self.model, self.slot, self.capacity)

    class Meta:
        verbose_name = u'硬盘'
        verbose_name_plural = u"硬盘"
        unique_together = ('asset', 'sn')

class NIC(models.Model):
    """网卡组件"""

    asset = models.ForeignKey('Asset',on_delete=models.CASCADE)  # 注意要用外键
    name = models.CharField(verbose_name=u'网卡名称', max_length=64, blank=True, null=True)
    model = models.CharField(verbose_name=u'网卡型号', max_length=128)
    mac = models.CharField(verbose_name=u'MAC地址', max_length=64)  # 虚拟机有可能会出现同样的mac地址
    ip_address = models.GenericIPAddressField(verbose_name=u'IP地址', blank=True, null=True)
    net_mask = models.CharField(verbose_name=u'掩码', max_length=64, blank=True, null=True)
    bonding = models.CharField(verbose_name=u'绑定地址', max_length=64, blank=True, null=True)

    def __str__(self):
        return '%s:  %s:  %s' % (self.asset.name, self.model, self.mac)

    class Meta:
        verbose_name = u'网卡'
        verbose_name_plural = u"网卡"
        unique_together = ('asset', 'model', 'mac')  # 资产、型号和mac必须联合唯一。防止虚拟机中的特殊情况发生错误


class EventLog(models.Model):
    """
    日志.
    在关联对象被删除的时候，不能一并删除，需保留日志。
    因此，on_delete=models.SET_NULL
    """
    name = models.CharField(verbose_name=u'事件名称', max_length=128)
    event_type_choice = (
        (0, u'增加'),
        (1, u'修改'),
        (2, u'删除'),
    )
    asset = models.ForeignKey('Asset',blank=True, null=True, on_delete=models.SET_NULL)
    datacenter = models.ForeignKey('DataCenter',blank=True, null=True, on_delete=models.SET_NULL)
    cabinet = models.ForeignKey('Cabinet',blank=True, null=True, on_delete=models.SET_NULL)
    event_type = models.SmallIntegerField(verbose_name=u'事件类型', choices=event_type_choice, default=4)
    reason = models.TextField(verbose_name=u'事件原因', max_length=256, blank=True, null=True)
    detail = models.TextField(verbose_name=u'事件详情')
    date = models.DateTimeField(verbose_name=u'事件时间', auto_now_add=True)
    user = models.ForeignKey(User, blank=True, null=True, verbose_name=u'事件执行人', on_delete=models.SET_NULL)
    memo = models.TextField(verbose_name=u'备注', blank=True, null=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'事件纪录'
        verbose_name_plural = u"事件纪录"


class IpEventLog(models.Model):
    """
    IP日志
    """
    event_type_choice = (
        (0, u'增加'),
        (1, u'修改'),
        (2, u'删除'),
    )
    ipnet = models.ForeignKey('IpNet',blank=True, null=True, on_delete=models.CASCADE)
    ipaddress = models.ForeignKey('IpAddress',blank=True, null=True, on_delete=models.CASCADE)
    event_type = models.SmallIntegerField(verbose_name=u'事件类型', choices=event_type_choice, default=4)
    reason = models.TextField(verbose_name=u'事件原因', max_length=256, blank=True, null=True)
    detail = models.TextField(verbose_name=u'事件详情')
    date = models.DateTimeField(verbose_name=u'事件时间', auto_now_add=True)
    user = models.ForeignKey(User, blank=True, null=True, verbose_name=u'事件执行人', on_delete=models.SET_NULL)

    def __str__(self):
        return self.detail

    class Meta:
        verbose_name = u'IP事件纪录'
        verbose_name_plural = u"IP事件纪录"


class NewAssetApprovalZone(models.Model):
    """新资产待审批区"""

    sn = models.CharField(verbose_name=u'资产SN号', max_length=128, unique=True)  # 此字段必填
    asset_type_choice = (
        ('server', u'服务器'),
        ('networkdevice', u'网络设备'),
        ('storagedevice', u'存储设备'),
        ('securitydevice', u'安全设备'),
        ('IDC', u'机房'),
    )
    asset_type = models.CharField(choices=asset_type_choice, default='server', max_length=64, blank=True, null=True,verbose_name=u'资产类型')
    manufacturer = models.CharField(max_length=64, blank=True, null=True, verbose_name=u'生产厂商')
    model = models.CharField(max_length=128, blank=True, null=True, verbose_name=u'型号')
    ram_size = models.PositiveIntegerField(blank=True, null=True, verbose_name=u'内存大小')
    cpu_model = models.CharField(max_length=128, blank=True, null=True, verbose_name=u'CPU型号')
    cpu_count = models.PositiveSmallIntegerField(blank=True, null=True)
    cpu_core_count = models.PositiveSmallIntegerField(blank=True, null=True)
    os_distribution = models.CharField(max_length=64, blank=True, null=True)
    os_type = models.CharField(max_length=64, blank=True, null=True)
    os_release = models.CharField(max_length=64, blank=True, null=True)
    data = models.TextField(verbose_name=u'资产数据')  # 此字段必填
    c_time = models.DateTimeField(verbose_name=u'汇报日期', auto_now_add=True)
    m_time = models.DateTimeField(verbose_name=u'数据更新日期', auto_now=True)
    approved = models.BooleanField(verbose_name=u'是否批准', default=False)

    def __str__(self):
        return self.sn

    class Meta:
        verbose_name = u'新上线待批准资产'
        verbose_name_plural = u"新上线待批准资产"
        ordering = ['-c_time']

class Circuit(models.Model):
    """专线表"""
    circuit_type_choice = (
        (0, 'Fiber'),
        (1, 'DWDM'),
        (2, 'LAN'),
        (3, 'SDH'),
    )
    circuit_status_choice = (
        (0, '运营'),
        (1, '拆除'),
        (2, '待启用'),
    )
    selfuse_status_choice = (
        (0, '是'),
        (1, '否'),
    )
    circuit_business_unit_choice = (
        (0, '领骏'),
        (1, '驰联'),
        )
    
    name = models.CharField(max_length=128, null=False,unique=True, verbose_name=u"专线名称")
    number = models.CharField(max_length=64,null=False,unique=True, verbose_name=u"专线编号")
    cloudnumber = models.CharField(max_length=64,verbose_name=u"云专线编号")
    circuit_type = models.SmallIntegerField(choices=circuit_type_choice, default=0, verbose_name=u"专线类型")
    circuit_lenth = models.FloatField(null=True,blank=True,verbose_name=u"长度(KM)")
    attenuation = models.CharField(max_length=64,null=True,blank=True,verbose_name=u"衰耗(db)")
    bw = models.IntegerField(null=True,blank=True,verbose_name=u"带宽(M)")
    machineroom_a = models.ForeignKey('MachineRoom',on_delete=models.CASCADE, null=True, blank=True,related_name='MachineRoom_A',verbose_name=u'A端机房')
    machineroom_z = models.ForeignKey('MachineRoom',on_delete=models.CASCADE, null=True, blank=True,related_name='MachineRoom_Z',verbose_name=u'Z端机房')
    cabinet_a = models.ForeignKey('Cabinet',on_delete=models.CASCADE, null=True, blank=True,related_name='Cabinet_A',verbose_name=u'A端机柜')
    cabinet_z = models.ForeignKey('Cabinet',on_delete=models.CASCADE, null=True, blank=True,related_name='Cabinet_Z',verbose_name=u'A端机柜')
    networkdevice_a = models.ForeignKey('Asset',on_delete=models.CASCADE, null=True, blank=True,related_name='Networkdevice_A',verbose_name=u'A端设备')
    networkdevice_z = models.ForeignKey('Asset',on_delete=models.CASCADE, null=True, blank=True,related_name='Networkdevice_Z',verbose_name=u'Z端设备')
    port_a = models.ForeignKey('Ports',on_delete=models.CASCADE, null=True, blank=True,related_name='Ports_A',verbose_name=u'A端设备端口')
    port_z = models.ForeignKey('Ports',on_delete=models.CASCADE, null=True, blank=True,related_name='Ports_Z',verbose_name=u'Z端设备端口')
    maintainer = models.ForeignKey(User, on_delete=models.CASCADE,null=True, blank=True,verbose_name=u'维护人')
    date_online = models.DateField(null=True,blank=True,verbose_name=u'启用日期')
    date_dismantle = models.DateField(null=True,blank=True,verbose_name=u'拆除日期')
    status = models.SmallIntegerField(choices=circuit_status_choice, default=0, verbose_name=u"专线状态")
    vlan = models.SmallIntegerField(null=True,blank=True,verbose_name=u"专线vlan")
    isp = models.CharField(max_length=32,verbose_name=u"专线运营商")
    isp_contacts = models.CharField(max_length=32,verbose_name=u"运营商联系人")
    isp_contacts_phonenumber = models.CharField(max_length=30,verbose_name=u"运营商联系电话")
    router = models.TextField(null=True,blank=True,verbose_name=u"专线路由")
    self_use_status = models.SmallIntegerField(choices=selfuse_status_choice,verbose_name=u"是否自用")
    customer = models.CharField(null=True,blank=True,max_length=32,verbose_name=u"客户名称")
    business_unit = models.SmallIntegerField(choices=circuit_business_unit_choice, default=0,null=True, blank=True, verbose_name=u'所属业务线')
    dwdm = models.ForeignKey('DWDM',on_delete=models.CASCADE, null=True, blank=True,verbose_name=u'DWDM')
    memo = models.TextField(blank=True,default='', verbose_name=u'备注')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'专线'
        verbose_name_plural = u"专线"

class CircuitEventLog(models.Model):
    """
    专线日志
    """
    event_type_choice = (
        (0, u'增加'),
        (1, u'修改'),
        (2, u'删除'),
    )
    circuit = models.ForeignKey('Circuit',blank=True, null=True, on_delete=models.CASCADE)
    event_type = models.SmallIntegerField(verbose_name=u'事件类型', choices=event_type_choice, default=4)
    reason = models.TextField(verbose_name=u'事件原因', max_length=256, blank=True, null=True)
    detail = models.TextField(verbose_name=u'事件详情')
    date = models.DateTimeField(verbose_name=u'事件时间', auto_now_add=True)
    user = models.ForeignKey(User, blank=True, null=True, verbose_name=u'事件执行人', on_delete=models.CASCADE)

    def __str__(self):
        return self.detail

    class Meta:
        verbose_name = u'专线事件纪录'
        verbose_name_plural = u"专线事件纪录"

class DWDM(models.Model):
    """DWDM"""
    dwdm_status_choice = (
        (0, '运营'),
        (1, '拆除'),
        (2, '待启用'),
    )
    oeo_choice = (
        (0, '是'),
        (1, '否'),
    )
    dwdm_business_unit_choice = (
        (0, '领骏'),
        (1, '驰联'),
        )
    olp_choice = (
        (0, '是'),
        (1, '否'),
    )    
    
    name = models.CharField(max_length=128, null=False,unique=True, verbose_name=u"DWDM名称")
    manufacturer = models.ForeignKey('Manufacturer',on_delete=models.CASCADE, null=True, blank=True, verbose_name=u'厂商')
    modelnumber = models.ForeignKey('Modelnumber',on_delete=models.CASCADE, null=True,blank=True, verbose_name=u'型号')    
    high = models.SmallIntegerField(verbose_name=u"高度(U)",null=True,blank=True)
    mux = models.CharField(max_length=64,null=True,blank=True,verbose_name=u"合波器")
    pa = models.CharField(max_length=64,null=True,blank=True,verbose_name=u"PA规格")
    ba = models.CharField(max_length=64,null=True,blank=True,verbose_name=u"PA规格")
    dcm = models.CharField(max_length=64,null=True,blank=True,verbose_name=u"色散补偿")
    machineroom_a = models.ForeignKey('MachineRoom',on_delete=models.CASCADE, null=True, blank=True,related_name='DWDMMachineRoom_A',verbose_name=u'A端机房')
    machineroom_z = models.ForeignKey('MachineRoom',on_delete=models.CASCADE, null=True, blank=True,related_name='DWDMMachineRoom_Z',verbose_name=u'Z端机房')
    cabinet_a = models.ForeignKey('Cabinet',on_delete=models.CASCADE, null=True, blank=True,related_name='DWDMCabinet_A',verbose_name=u'A端机柜')
    cabinet_z = models.ForeignKey('Cabinet',on_delete=models.CASCADE, null=True, blank=True,related_name='DWDMCabinet_Z',verbose_name=u'A端机柜')
    device_a_number = models.ForeignKey('Asset',on_delete=models.CASCADE,null=True,blank=True,related_name='DWDMNetworkdevice_A',verbose_name=u"A端设备固资号")
    device_z_number = models.ForeignKey('Asset',on_delete=models.CASCADE,null=True,blank=True,related_name='DWDMNetworkdevice_Z',verbose_name=u"Z端设备固资号")
    #device_a_sn = models.CharField(max_length=64, unique=True,null=True,blank=True,verbose_name=u"A端设备SN")
    #device_z_sn = models.CharField(max_length=64, unique=True,null=True,blank=True,verbose_name=u"Z端设备SN")
    maintainer = models.ForeignKey(User, on_delete=models.CASCADE,null=True, blank=True,verbose_name=u'维护人')
    status = models.SmallIntegerField(choices=dwdm_status_choice, default=0, verbose_name=u"DWDM状态")
    oeo = models.SmallIntegerField(choices=oeo_choice,verbose_name=u"是否有OEO")
    olp = models.SmallIntegerField(choices=olp_choice,verbose_name=u"是否有OLP")
    business_unit = models.SmallIntegerField(choices=dwdm_business_unit_choice,null=True, blank=True, verbose_name=u'所属业务线')
    memo = models.TextField(blank=True,default='', verbose_name=u'备注')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'DWDM'
        verbose_name_plural = u"DWDM"


class DwdmEventLog(models.Model):
    """
    专线日志
    """
    event_type_choice = (
        (0, u'增加'),
        (1, u'修改'),
        (2, u'删除'),
    )
    dwdm = models.ForeignKey('DWDM',blank=True, null=True, on_delete=models.CASCADE)
    channel = models.ForeignKey('Channel',blank=True, null=True, on_delete=models.CASCADE)
    event_type = models.SmallIntegerField(verbose_name=u'事件类型', choices=event_type_choice, default=4)
    reason = models.TextField(verbose_name=u'事件原因', max_length=256, blank=True, null=True)
    detail = models.TextField(verbose_name=u'事件详情')
    date = models.DateTimeField(verbose_name=u'事件时间', auto_now_add=True)
    user = models.ForeignKey(User, blank=True, null=True, verbose_name=u'事件执行人', on_delete=models.CASCADE)

    def __str__(self):
        return self.detail

    class Meta:
        verbose_name = u'DWDM事件纪录'
        verbose_name_plural = u"DWDM事件纪录"

class Channel(models.Model):
    """波道"""
    channel_status_choice = (
        (0, '运营'),
        (1, '拆除'),
        (2, '待启用'),
    )   
    
    name = models.CharField(max_length=128, null=False,verbose_name=u"波道名称")
    dwdm = models.ForeignKey('DWDM',on_delete=models.CASCADE,verbose_name=u'DWDM')
    customer = models.CharField(null=True,blank=True,max_length=64,verbose_name=u"客户名称")
    status = models.SmallIntegerField(choices=channel_status_choice, verbose_name=u"波道状态")
    networkdevice_a = models.ForeignKey('Asset',on_delete=models.CASCADE, null=True, blank=True,related_name='Channel_Networkdevice_A',verbose_name=u'A端设备')
    networkdevice_z = models.ForeignKey('Asset',on_delete=models.CASCADE, null=True, blank=True,related_name='Channel_Networkdevice_Z',verbose_name=u'Z端设备')
    port_a = models.ForeignKey('Ports',on_delete=models.CASCADE, null=True, blank=True,related_name='Channel_Ports_A',verbose_name=u'A端设备端口')
    port_z = models.ForeignKey('Ports',on_delete=models.CASCADE, null=True, blank=True,related_name='Channel_Ports_Z',verbose_name=u'Z端设备端口')
    memo = models.TextField(blank=True,default='', verbose_name=u'备注')

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = u'波道'
        verbose_name_plural = u"波道"

class ConIpNet(models.Model):
    """互联网段表"""
    ipnet_status_choice = (
        (0, '运营'),
        (1, '待分配'),
        (2, '已分配'),
        (3, '保留'),
    )    
    ipnet_type_choice =(
        (0, 'IDC'),
        (1, 'IXP'),
        (2, '管理'),
    )
    zone_choice =(
        (0, '上海'),
        (1, '北京'),
        (2, '广州'),
    )

    name = models.CharField(verbose_name=u'网段名称', unique=True,max_length=64)
    ipnet_status = models.SmallIntegerField(choices=ipnet_status_choice,verbose_name=u"网段状态")    
    ipnet_type = models.SmallIntegerField(choices=ipnet_type_choice,verbose_name=u"网段类型")
    zone = models.SmallIntegerField(choices=zone_choice,verbose_name=u'区域')
    networkdevice_a = models.ForeignKey('Asset',on_delete=models.CASCADE, null=True, blank=True,related_name='ConIpNet_Networkdevice_A',verbose_name=u'A端设备')
    networkdevice_z = models.ForeignKey('Asset',on_delete=models.CASCADE, null=True, blank=True,related_name='ConIpNet_Networkdevice_Z',verbose_name=u'Z端设备')
    port_a = models.CharField(verbose_name=u'起始端端口', max_length=32)
    port_z = models.CharField(verbose_name=u'终止端端口', max_length=32)
    ip_a = models.GenericIPAddressField(verbose_name=u'起始端IP',unique=True,null=True,blank=True,max_length=64)
    ip_z = models.GenericIPAddressField(verbose_name=u'终止端IP',unique=True,null=True,blank=True,max_length=64)
    vlan = models.SmallIntegerField(verbose_name=u"VLAN",null=True,blank=True)
    usage = models.CharField(verbose_name=u'用途', max_length=128)
    memo = models.CharField(verbose_name=u'备注', max_length=128, blank=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '互联网段'
        verbose_name_plural = "互联网段"
